package com.xuxin.pda.activity;

import android.Manifest;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xuxin.pda.R;
import com.xuxin.pda.base.BaseActivity;
import com.xuxin.pda.base.BaseConstant;
import com.xuxin.pda.glide.GlideApp;
import com.xuxin.pda.utils.ACache;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import butterknife.BindView;

public class SplashActivity extends BaseActivity {
    @BindView(R.id.splash_image)
    ImageView splash_image;

    private String userId;
    private String userName;
    private String userNo;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_splash);
    }

    @Override
    public void initView() {
        GlideApp.with(context).load(R.mipmap.img_splash).centerCrop().into(splash_image);
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(context);
        userId = aCache.getAsString(BaseConstant.userId);
        userName = aCache.getAsString(BaseConstant.userName);
        userNo = aCache.getAsString(BaseConstant.userNo);
        permissions();
    }

    @Override
    public void onComplete() {
    }

    @Override
    public void onClick(View view) {
    }

    private final Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NotNull Message message) {
            if (message.what == 0) {
                welcome();
            }
            return false;
        }
    });


    private void welcome() {
        if (!StringUtils.isEmpty(userId) && !StringUtils.isEmpty(userName) && !StringUtils.isEmpty(userNo)) {
            ActivityUtils.startActivity(LoginActivity.class);
        } else {
            ActivityUtils.startActivity(LoginActivity.class);
        }
        finish();
    }

    /**
     * 申请读写权限
     */
    public String[] permission = new String[]{
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.CAMERA};

    private void permissions() {
        PermissionUtils.permissionGroup(permission).callback(new PermissionUtils.FullCallback() {
            @Override
            public void onGranted(@NonNull List<String> granted) {
                mHandler.sendEmptyMessageDelayed(0, 1000);
            }

            @Override
            public void onDenied(@NonNull List<String> deniedForever, @NonNull List<String> denied) {
                ToastUtils.showShort("权限已被拒绝，请打开权限");
                finish();
            }
        }).request();
    }
}